package com.yuchen.ishop.web.app.buyer;

import com.yuchen.ishop.entity.BuyerAddress;
import com.yuchen.ishop.service.buyer.BuyerAddressRestClient;
import com.yuchen.ishop.vo.CookieUserInfo;
import com.yuchen.ishop.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping(value = "/my/buyer/address")
public class BuyerAddressAction extends BaseAction{

	@Autowired
    BuyerAddressRestClient buyerAddressRestClient;
	
	@RequestMapping(value = "index")
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        CookieUserInfo userInfo  = cookie2UserInfo(request);
        modelMap.addAttribute("list", buyerAddressRestClient.getByBuyerId(userInfo.getId()));
        modelMap.addAttribute("myMenu", "myAddress");
        return "my/buyer/myAddress";
    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public String insert(HttpServletRequest request, BuyerAddress buyerAddress, ModelMap modelMap) {
        CookieUserInfo userInfo  = cookie2UserInfo(request);
        buyerAddress.setBuyerId(userInfo.getId());
        buyerAddress.setCreateTime(getTime());
        buyerAddress.setUpdateTime(getTime());
        buyerAddressRestClient.insert(buyerAddress);
        return "redirect:/my/buyer/address/index.htm";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(BuyerAddress buyerAddress, ModelMap modelMap) {
        buyerAddressRestClient.update(buyerAddress);
        return "redirect:/my/buyer/address/index.htm";
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public String delete(Long id, ModelMap modelMap) {
        buyerAddressRestClient.delete(id);
        return "redirect:/my/buyer/address/index.htm";
    }

    @RequestMapping(value = "get")
    @ResponseBody
    public Object get(Long id, ModelMap modelMap) {
        BuyerAddress buyerAddress = buyerAddressRestClient.getById(id);
        return buyerAddress;
    }
}
